package test.xstream;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class ArrayConverter implements Converter {

        public boolean canConvert(Class clazz) {
        	System.out.println(clazz.getSimpleName());
                return clazz.equals(ArrayList.class);
        }

        public void marshal(Object value, HierarchicalStreamWriter writer,
                        MarshallingContext context) 
        {
        	AbstractList<HashMap<String,Object>> arr = (AbstractList<HashMap<String,Object>>) value;
        	writer.startNode("list");
        	for(HashMap<String,Object> map : arr)
        	{
        		
	        	for (Entry<String,Object> entry : map.entrySet()) {
	        		
	                writer.startNode(entry.getKey().toString());
	                writer.setValue(entry.getValue().toString());
	                writer.endNode();
                
	        	}
	        	
        	}
        	writer.endNode();
        }

        public Object unmarshal(HierarchicalStreamReader reader,
                        UnmarshallingContext context) {
        	
        	
        	System.out.println(reader.toString());
        	 return null;
        }

}